App({
  todos: [
    { text: 'Learning Javascript', completed: true },
    { text: 'Learning ES2016', completed: true },
    { text: 'Learning 支付宝小程序', completed: false },
  ],
  onLaunch(options) {
    
  },
  userInfo: null,
  getUserInfo() {
    
  },
  memberLogin(){
    var that = this;
    my.getAuthCode({
      scopes: ['auth_user'],
      success: (authcode) => {
        my.httpRequest({
          url: that.geturl+'/member/login-by-ali-sp',
          method: 'POST',
          data:{
            code:authcode.authCode
          },
          dataType: 'json',
          success: function(res) {
            if(res.data.msg=='登录成功'){  
              console.info('登录成功');       
              my.setStorage({
                key: 'token',
                data: {
                  token:res.data.data.token,
                  time:600
                },
                success: function() {
                  my.getAuthUserInfo({
                    success: (res) => {
                      my.setStorage({
                        key: 'memberInfo',
                        data: {
                          avatar:res.avatar,
                          nickName:res.nickName,
                        }
                      });
                    }
                  });
                }
              });
            }else{
              console.info('登录失败');
              my.removeStorage({
                key: 'token'
              });
            }
          }
        });
      }
    });
  },
  checkToken(){
    my.getStorage({
      key: 'token',
      success: function(res) {      
        if(typeof res.data=='undefined' || res.data==null){
          my.navigateTo({ url: '../todos/todos' });
        }
      }
    });
  },
  checkToken(){
    setInterval(this.monitorData,1000);
  },
  monitorData(){
    var that = this;
    my.getStorage({
      key: 'token',
      success: function(res) {  
        if(typeof res.data!='undefined' || res.data!=null){
          if(res.data.time<0){
            my.getAuthCode({
              scopes: ['auth_user'],
              success: (authcode) => {
                my.getAuthUserInfo({
                  success: (res) => {
                    my.setStorage({
                      key: 'memberInfo',
                      data: {
                        avatar:res.avatar,
                        nickName:res.nickName,
                      }
                    });
                  }
                });
                my.httpRequest({
                  url: that.geturl+'/member/login-by-ali-sp',
                  method: 'POST',
                  data:{
                    code:authcode.authCode
                  },
                  dataType: 'json',
                  success: function(res) {
                    my.setStorage({
                      key: 'token',
                      data: {
                        token:res.data.data.token,
                        time:600
                      },
                      success: (authcode) => {
                        console.info('写入成功');
                      }
                    });
                  }
                });
              }
            });
          }else{
            my.setStorageSync({
              key: 'token',
              data: {
                time:res.data.time-1,
                token:res.data.token            }
            });
          }
        }
      }
    });
  },
  geturl:'https://api.hualiyue.com',

});
